<!DOCTYPE html>
<html lang="zh-CN">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="robots" content="noodp" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1">
        <title>Shell编程基础01 - 德国粗茶淡饭</title><meta name="Description" content="Shell编程基础01"><meta property="og:title" content="Shell编程基础01" />
<meta property="og:description" content="Shell编程基础01" />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://www.ctq6.cn/technology/linux/shell%E7%BC%96%E7%A8%8B%E5%9F%BA%E7%A1%8001/" />
<meta property="og:image" content="https://www.ctq6.cn/logo.png"/>
<meta property="article:published_time" content="2021-02-16T19:09:22+08:00" />
<meta property="article:modified_time" content="2021-02-16T19:09:22+08:00" />
<meta name="twitter:card" content="summary_large_image"/>
<meta name="twitter:image" content="https://www.ctq6.cn/logo.png"/>

<meta name="twitter:title" content="Shell编程基础01"/>
<meta name="twitter:description" content="Shell编程基础01"/>
<meta name="application-name" content="LoveIt">
<meta name="apple-mobile-web-app-title" content="LoveIt"><meta name="theme-color" content="#ffffff"><meta name="msapplication-TileColor" content="#da532c"><link rel="shortcut icon" type="image/x-icon" href="/favicon.ico" />
        <link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
        <link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png"><link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png"><link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5"><link rel="manifest" href="/site.webmanifest"><link rel="canonical" href="https://www.ctq6.cn/technology/linux/shell%E7%BC%96%E7%A8%8B%E5%9F%BA%E7%A1%8001/" /><link rel="prev" href="https://www.ctq6.cn/technology/linux/keepalived_haproxy%E9%AB%98%E5%8F%AF%E7%94%A8/" /><link rel="next" href="https://www.ctq6.cn/technology/linux/shell%E7%BC%96%E7%A8%8B%E5%9F%BA%E7%A1%8003/" /><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/normalize.css@8.0.1/normalize.min.css"><link rel="stylesheet" href="/css/style.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.13.0/css/all.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.7.2/animate.min.css"><script type="application/ld+json">
    {
        "@context": "http://schema.org",
        "@type": "BlogPosting",
        "headline": "Shell编程基础01",
        "inLanguage": "zh-CN",
        "mainEntityOfPage": {
            "@type": "WebPage",
            "@id": "https:\/\/www.ctq6.cn\/technology\/linux\/shell%E7%BC%96%E7%A8%8B%E5%9F%BA%E7%A1%8001\/"
        },"image": [{
                            "@type": "ImageObject",
                            "url": "https:\/\/www.ctq6.cn\/images\/Apple-Devices-Preview.png",
                            "width":  3200 ,
                            "height":  2048 
                        }],"genre": "technology","keywords": "Linux","wordcount":  2924 ,
        "url": "https:\/\/www.ctq6.cn\/technology\/linux\/shell%E7%BC%96%E7%A8%8B%E5%9F%BA%E7%A1%8001\/","datePublished": "2021-02-16T19:09:22+08:00","dateModified": "2021-02-16T19:09:22+08:00","license": "This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.","publisher": {
            "@type": "Organization",
            "name": "xxxx","logo": {
                    "@type": "ImageObject",
                    "url": "https:\/\/www.ctq6.cn\/images\/avatar.png",
                    "width":  528 ,
                    "height":  560 
                }},"author": {
                "@type": "Person",
                "name": "MikelPan"
            },"description": "Shell编程基础01"
    }
    </script></head>
    <body header-desktop="fixed" header-mobile="auto"><script type="text/javascript">(window.localStorage && localStorage.getItem('theme') ? localStorage.getItem('theme') === 'dark' : ('auto' === 'auto' ? window.matchMedia('(prefers-color-scheme: dark)').matches : 'auto' === 'dark')) && document.body.setAttribute('theme', 'dark');</script>

        <div id="mask"></div><div class="wrapper"><header class="desktop" id="header-desktop">
    <div class="header-wrapper">
        <div class="header-title">
            <a href="/" title="德国粗茶淡饭"><span class="header-title-pre"><i class='far fa-kiss-wink-heart fa-fw'></i></span>德国粗茶淡饭</a>
        </div>
        <div class="menu">
            <div class="menu-inner"><a class="menu-item" href="/posts/"> 生活 </a><a class="menu-item" href="/technology/"> 技术 </a><a class="menu-item" href="/tags/"> 标签 </a><a class="menu-item" href="/categories/"> 分类 </a><a class="menu-item" href="/categories/documentation/"> 文档 </a><a class="menu-item" href="/about/"> 关于 </a><a class="menu-item" href="https://github.com/MikelPan/Cnblog.git" title="GitHub" rel="noopener noreffer" target="_blank"><i class='fab fa-github fa-fw'></i>  </a><span class="menu-item delimiter"></span><a href="javascript:void(0);" class="menu-item language" title="选择语言">简体中文<i class="fas fa-chevron-right fa-fw"></i>
                        <select class="language-select" id="language-select-desktop" onchange="location = this.value;"><option value="/technology/linux/shell%E7%BC%96%E7%A8%8B%E5%9F%BA%E7%A1%8001/" selected>简体中文</option></select>
                    </a><span class="menu-item search" id="search-desktop">
                        <input type="text" placeholder="搜索文章标题或内容..." id="search-input-desktop">
                        <a href="javascript:void(0);" class="search-button search-toggle" id="search-toggle-desktop" title="搜索">
                            <i class="fas fa-search fa-fw"></i>
                        </a>
                        <a href="javascript:void(0);" class="search-button search-clear" id="search-clear-desktop" title="清空">
                            <i class="fas fa-times-circle fa-fw"></i>
                        </a>
                        <span class="search-button search-loading" id="search-loading-desktop">
                            <i class="fas fa-spinner fa-fw fa-spin"></i>
                        </span>
                    </span><a href="javascript:void(0);" class="menu-item theme-switch" title="切换主题">
                    <i class="fas fa-adjust fa-fw"></i>
                </a>
            </div>
        </div>
    </div>
</header><header class="mobile" id="header-mobile">
    <div class="header-container">
        <div class="header-wrapper">
            <div class="header-title">
                <a href="/" title="德国粗茶淡饭"><span class="header-title-pre"><i class='far fa-kiss-wink-heart fa-fw'></i></span>德国粗茶淡饭</a>
            </div>
            <div class="menu-toggle" id="menu-toggle-mobile">
                <span></span><span></span><span></span>
            </div>
        </div>
        <div class="menu" id="menu-mobile"><div class="search-wrapper">
                    <div class="search mobile" id="search-mobile">
                        <input type="text" placeholder="搜索文章标题或内容..." id="search-input-mobile">
                        <a href="javascript:void(0);" class="search-button search-toggle" id="search-toggle-mobile" title="搜索">
                            <i class="fas fa-search fa-fw"></i>
                        </a>
                        <a href="javascript:void(0);" class="search-button search-clear" id="search-clear-mobile" title="清空">
                            <i class="fas fa-times-circle fa-fw"></i>
                        </a>
                        <span class="search-button search-loading" id="search-loading-mobile">
                            <i class="fas fa-spinner fa-fw fa-spin"></i>
                        </span>
                    </div>
                    <a href="javascript:void(0);" class="search-cancel" id="search-cancel-mobile">
                        取消
                    </a>
                </div><a class="menu-item" href="/posts/" title="">生活</a><a class="menu-item" href="/technology/" title="">技术</a><a class="menu-item" href="/tags/" title="">标签</a><a class="menu-item" href="/categories/" title="">分类</a><a class="menu-item" href="/categories/documentation/" title="">文档</a><a class="menu-item" href="/about/" title="">关于</a><a class="menu-item" href="https://github.com/MikelPan/Cnblog.git" title="GitHub" rel="noopener noreffer" target="_blank"><i class='fab fa-github fa-fw'></i></a><a href="javascript:void(0);" class="menu-item theme-switch" title="切换主题">
                <i class="fas fa-adjust fa-fw"></i>
            </a><a href="javascript:void(0);" class="menu-item" title="选择语言">简体中文<i class="fas fa-chevron-right fa-fw"></i>
                    <select class="language-select" onchange="location = this.value;"><option value="/technology/linux/shell%E7%BC%96%E7%A8%8B%E5%9F%BA%E7%A1%8001/" selected>简体中文</option></select>
                </a></div>
    </div>
</header>
<div class="search-dropdown desktop">
    <div id="search-dropdown-desktop"></div>
</div>
<div class="search-dropdown mobile">
    <div id="search-dropdown-mobile"></div>
</div>
<main class="main">
                <div class="container"><div class="page single special"><h1 class="single-title animated pulse faster">Shell编程基础01</h1><div class="content" id="content"><h2 id="前言">前言</h2>
<h3 id="为什么学shell">为什么学Shell</h3>
<p>Shell脚本语言是实现Linux/UNIX系统管理及自动化运维所必备的重要工具， Linux/UNIX系统的底层及基础应用软件的核心大都涉及Shell脚本的内容。每一个合格 的Linux系统管理员或运维工程师，都需要能够熟练地编写Shell脚本语言，并能够阅 读系统及各类软件附带的Shell脚本内容。只有这样才能提升运维人员的工作效率，适 应曰益复杂的工作环境，减少不必要的重复工作，从而为个人的职场发展奠定较好的基础</p>
<h3 id="什么是shell">什么是shell</h3>
<p>Shell是一个命令解释器，它在操作系统的最外层，负责直接与用户对话，把用户的输入解释给操作系统，并处理各种各样的操作系统的输出结果，输出屏幕返回给用户。</p>
<p>这种对话方式可以是：</p>
<p>交互的方式：从键盘输入命令，通过/bin/bash的解析，可以立即得到Shell的回应.</p>
<h3 id="什么是shell脚本">什么是shell脚本</h3>
<p>命令、变量和流程控制语句等有机的结合起来，shell脚本擅长处理纯文本类型的数据，而linux中，几乎所有的配置文件，日志，都是纯文本类型文件。</p>
<h3 id="脚本语言的分类">脚本语言的分类</h3>
<p>一、编译型语言</p>
<p>定义：指用专用的编译器，针对特定的操作平台（操作系统）将某种高级语言源代码一次性翻译成可被硬件平台直接运行的二进制机器码（具有操作数，指令、及相应的格式），这个过程叫做编译（./configure  make makeinstall ）；编译好的可执行性文件（.exe），可在相对应的平台上运行（移植性差，但运行效率高）。。</p>
<p>典型的编译型语言有， C语言、C++等。</p>
<p>另外，Java语言是一门很特殊的语言，Java程序需要进行编译步骤，但并不会生成特定平台的二进制机器码，它编译后生成的是一种与平台无关的字节码文件（*.class）（移植性好的原因），这种字节码自然不能被平台直接执行，运行时需要由解释器解释成相应平台的二进制机器码文件；大多数人认为Java是一种编译型语言，但我们说Java即是编译型语言，也是解释型语言也并没有错。</p>
<p>二、解释型语言</p>
<p>定义：指用专门解释器对源程序逐行解释成特定平台的机器码并立即执行的语言；相当于把编译型语言的编译链接过程混到一起同时完成的。</p>
<p>解释型语言执行效率较低，且不能脱离解释器运行，但它的跨平台型比较容易，只需提供特定解释器即可。</p>
<p>常见的解释型语言有， Python（同时是脚本语言）与Ruby等。</p>
<p>三、脚本语言</p>
<p>定义：为了缩短传统的编写-编译-链接-运行（edit-compile-link-run）过程而创建的计算机编程语言。</p>
<p>特点：程序代码即是最终的执行文件，只是这个过程需要解释器的参与，所以说脚本语言与解释型语言有很大的联系。脚本语言通常是被解释执行的，而且程序是文本文件。</p>
<p>典型的脚本语言有，JavaScript，Python，shell等。</p>
<p>其他常用的脚本语句种类</p>
<p>PHP是网页程序，也是脚本语言。是一款更专注于web页面开发（前端展示）的脚本语言，例如：Dedecms,discuz。PHP程序也可以处理系统日志，配置文件等，php也可以调用系统命令。</p>
<p>Perl脚本语言。比shell脚本强大很多，语法灵活、复杂，实现方式很多，不易读，团队协作困难，但仍不失为很好的脚本语言，存世大量的程序软件。MHA高可用Perl写的</p>
<p>Python，不但可以做脚本程序开发，也可以实现web程序以及软件的开发。近两年越来越多的公司都会要求会Python。</p>
<p>Shell脚本与php/perl/python语言的区别和优势？</p>
<p>shell脚本的优势在于处理操作系统底层的业务 （linux系统内部的应用都是shell脚本完成）因为有大量的linux系统命令为它做支撑。2000多个命令都是shell脚本编程的有力支撑，特别是grep、awk、sed等。例如：一键软件安装、优化、监控报警脚本，常规的业务应用，shell开发更简单快速，符合运维的简单、易用、高效原则.</p>
<p>PHP、Python优势在于开发运维工具以及web界面的管理工具，web业务的开发等。处理一键软件安装、优化，报警脚本。常规业务的应用等php/python也是能够做到的。但是开发效率和复杂比用shell就差很多了。</p>
<h3 id="系统中的shell">系统中的shell</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-bash" data-lang="bash">cat /etc/shells 
/bin/sh
/bin/bash
/usr/bin/sh
/usr/bin/bash
</code></pre></td></tr></table>
</div>
</div><h2 id="脚本书写规范">脚本书写规范</h2>
<h3 id="脚本统一存放目录">脚本统一存放目录</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-bash" data-lang="bash">mkdir -p /services/scripts<span class="p">;</span><span class="nb">cd</span> /services/scripts
</code></pre></td></tr></table>
</div>
</div><h3 id="编辑脚本使用vim">编辑脚本使用vim</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-bash" data-lang="bash"><span class="c1"># cat  ~/.vimrc </span>
autocmd BufNewFile *.py,*.go,*.sh,*.java <span class="nb">exec</span> <span class="s2">&#34;:call SetTitle()&#34;</span>

func SetTitle<span class="o">()</span>
    <span class="k">if</span> expand<span class="o">(</span><span class="s2">&#34;%:e&#34;</span><span class="o">)</span> <span class="o">==</span> <span class="s1">&#39;sh&#39;</span>
        call setline<span class="o">(</span>1,<span class="s2">&#34;#!/bin/bash&#34;</span><span class="o">)</span>
        call setline<span class="o">(</span>2, <span class="s2">&#34;##############################################################&#34;</span><span class="o">)</span>
        call setline<span class="o">(</span>3, <span class="s2">&#34;# File Name: &#34;</span>.expand<span class="o">(</span><span class="s2">&#34;%&#34;</span><span class="o">))</span>
        call setline<span class="o">(</span>4, <span class="s2">&#34;# Version: V1.0&#34;</span><span class="o">)</span>
        call setline<span class="o">(</span>5, <span class="s2">&#34;# Author: Mikel_Pan&#34;</span><span class="o">)</span>
        call setline<span class="o">(</span>6, <span class="s2">&#34;# Organization: https://github.com/plyxgit/Cnblog.git&#34;</span><span class="o">)</span>
        call setline<span class="o">(</span>7, <span class="s2">&#34;# Created Time : &#34;</span>.strftime<span class="o">(</span><span class="s2">&#34;%F %T&#34;</span><span class="o">))</span>
        call setline<span class="o">(</span>8, <span class="s2">&#34;# Description:&#34;</span><span class="o">)</span>
        call setline<span class="o">(</span>9, <span class="s2">&#34;##############################################################&#34;</span><span class="o">)</span>
        call setline<span class="o">(</span>10, <span class="s2">&#34;&#34;</span><span class="o">)</span>
    endif
endfunc
</code></pre></td></tr></table>
</div>
</div><h3 id="文件名规范">文件名规范</h3>
<p>名字要有意义，并且结尾以 .sh 结束</p>
<h3 id="开发的规范和习惯小结">开发的规范和习惯小结</h3>
<ol>
<li>
<p>放在统一的目录</p>
</li>
<li>
<p>脚本以.sh为扩展名</p>
</li>
<li>
<p>开头指定脚本解释器。</p>
</li>
<li>
<p>开头加版本版权等信息，可配置~/.vimrc文件自动添加。</p>
</li>
<li>
<p>脚本不要用中文注释，尽量用英文注释。</p>
</li>
<li>
<p>代码书写优秀习惯</p>
</li>
</ol>
<p>a、成对的内容一次性写出来，防止遗漏，如[  ]、' &lsquo;、&quot; &ldquo;等</p>
<p>b、[  ]两端要有空格，先输入[  ],退格，输入2个空格，再退格写。</p>
<p>c、流程控制语句一次书写完，再添加内容。(if 条件 ; then  内容;fi)ddd</p>
<p>d、通过缩进让代码易读。</p>
<p>f、脚本中的引号都是英文状态下的引号，其他字符也是英文状态。</p>
<h2 id="shell脚本的执行">shell脚本的执行</h2>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-bash" data-lang="bash">sh/bash   scripts.sh 
chown +x   ./scripts.sh  <span class="o">&amp;&amp;</span> ./scripts.sh  
<span class="nb">source</span> scripts.sh
. <span class="o">(</span>空格<span class="o">)</span> scripts.sh
cat oldboyedu.sh <span class="p">|</span>bash  <span class="c1"># 效率较低</span>
</code></pre></td></tr></table>
</div>
</div><p><em>source 与 .（点） 的作用</em></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-bash" data-lang="bash"><span class="c1"># help source  |head -2</span>
source: <span class="nb">source</span> 文件名 <span class="o">[</span>参数<span class="o">]</span>
    在当前 shell 中执行一个文件中的命令。
</code></pre></td></tr></table>
</div>
</div><p>.(点)</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-bash" data-lang="bash"><span class="nb">help</span> . <span class="p">|</span>head -2
.: . 文件名 <span class="o">[</span>参数<span class="o">]</span>
    在当前 shell 中执行一个文件中的命令。
</code></pre></td></tr></table>
</div>
</div><h2 id="shell-的变量">shell 的变量</h2>
<h3 id="什么是变量">什么是变量</h3>
<p>变量可以分为两类：环境变量（全局变量）和普通变量（局部变量）</p>
<p>环境变量也可称为全局变量，可以在创建他们的Shell及其派生出来的任意子进程shell中使用，环境变量又可分为自定义环境变量和Bash内置的环境变量</p>
<p>普通变量也可称为局部变量，只能在创建他们的Shell函数或Shell脚本中使用。普通变量一般是由开发者用户开发脚本程序时创建的。</p>
<p>特殊变量</p>
<h3 id="环境变量">环境变量</h3>
<p>使用 env/declare/set/export -p 命令查看系统中的环境变量，这三个命令的的输出方式稍有不同。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span><span class="lnt">8
</span><span class="lnt">9
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-bash" data-lang="bash"><span class="c1"># env</span>
<span class="nv">XDG_SESSION_ID</span><span class="o">=</span><span class="m">6249</span>
<span class="nv">HOSTNAME</span><span class="o">=</span>kube-master
<span class="nv">TERM</span><span class="o">=</span>xterm
<span class="nv">SHELL</span><span class="o">=</span>/bin/bash
<span class="nv">HISTSIZE</span><span class="o">=</span><span class="m">1000</span>
<span class="nv">SSH_CLIENT</span><span class="o">=</span>14.103.36.188 <span class="m">56875</span> <span class="m">22</span>
<span class="nv">SSH_TTY</span><span class="o">=</span>/dev/pts/0
<span class="nv">USER</span><span class="o">=</span>root
</code></pre></td></tr></table>
</div>
</div><p>输出一个系统中的 环境变量</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-bash" data-lang="bash"><span class="o">[</span>root@kube-master ~<span class="o">]</span><span class="c1"># echo $HOSTNAME</span>
kube-master
</code></pre></td></tr></table>
</div>
</div><h3 id="普通变量">普通变量</h3>
<p>本地变量在用户当前的Shell生存期的脚本中使用。例如，本地变量OLDBOY取值为bingbing，这个值在用户当前Shell生存期中有意义。如果在Shell中启动另一个进程或退出，本地变量值将无效.</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-bash" data-lang="bash"><span class="nv">a</span><span class="o">=</span>1<span class="p">;</span><span class="nb">echo</span> <span class="nv">$a</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="export命令">export命令</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-bash" data-lang="bash"><span class="c1"># help export </span>
export: <span class="nb">export</span> <span class="o">[</span>-fn<span class="o">]</span> <span class="o">[</span>名称<span class="o">[=</span>值<span class="o">]</span> ...<span class="o">]</span> 或 <span class="nb">export</span> -p
为 shell 变量设定导出属性。

标记每个 NAME 名称为自动导出到后续命令执行的环境。如果提供了 VALUE
则导出前将 VALUE 作为赋值。
</code></pre></td></tr></table>
</div>
</div><p>export 命令说明：
当前shell窗口及子shell窗口生效
在新开的shell窗口不会生效，生效需要写入配置文件</p>
<h3 id="环境变量相关配置文件">环境变量相关配置文件</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-bash" data-lang="bash">/etc/proflie
/etc/bashrc
~/.bashrc
~/.bash_profile
/etc/proflie.d/  <span class="c1"># 目录</span>
</code></pre></td></tr></table>
</div>
</div><p>文件读取顺序：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-bash" data-lang="bash">① /etc/profile
② ~/.bash_profile
③ ~/.bashrc
④ /etc/bashrc
</code></pre></td></tr></table>
</div>
</div><h3 id="环境变量的知识小结">环境变量的知识小结</h3>
<ul>
<li>变量名通常要大写。</li>
<li>变量可以在自身的Shell及子Shell中使用。</li>
<li>常用export来定义环境变量。</li>
<li>执行env默认可以显示所有的环境变量名称及对应的值。</li>
<li>输出时用“$变量名”，取消时用“unset变量名”。</li>
<li>书写crond定时任务时要注意，脚本要用到的环境变量最好先在所执行的Shell脚本中重新定义。</li>
<li>如果希望环境变量永久生效，则可以将其放在用户环境变量文件或全局环境变量文件里。</li>
</ul>
</div><div id="comments"></div></div></div>
            </main><footer class="footer">
        <div class="footer-container"><div class="footer-line"><i class="far fa-copyright fa-fw"></i><span itemprop="copyrightYear">2019 - 2021</span><span class="author" itemprop="copyrightHolder">&nbsp;<a href="/" target="_blank">mikel pan</a></span>&nbsp;|&nbsp;<span class="license"><a rel="license external nofollow noopener noreffer" href="https://creativecommons.org/licenses/by-nc/4.0/" target="_blank">CC BY-NC 4.0</a></span><span class="icp-splitter">&nbsp;|&nbsp;</span><br class="icp-br"/>
                    <span class="icp"><a href="https://beian.miit.gov.cn/" target="_blank">粤ICP备2021047442号</a></span></div>
        </div>
    </footer></div>

        <div id="fixed-buttons"><a href="#" id="back-to-top" class="fixed-button" title="回到顶部">
                <i class="fas fa-arrow-up fa-fw"></i>
            </a><a href="#" id="view-comments" class="fixed-button" title="查看评论">
                <i class="fas fa-comment fa-fw"></i>
            </a>
        </div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/lightgallery.js@1.2.0/dist/css/lightgallery.min.css"><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/smooth-scroll@16.1.3/dist/smooth-scroll.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/autocomplete.js@0.37.1/dist/autocomplete.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/algoliasearch@4.2.0/dist/algoliasearch-lite.umd.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/lazysizes@5.2.2/lazysizes.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/lightgallery.js@1.2.0/dist/js/lightgallery.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/lg-thumbnail.js@1.2.0/dist/lg-thumbnail.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/lg-zoom.js@1.2.0/dist/lg-zoom.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/clipboard@2.0.6/dist/clipboard.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/sharer.js@0.4.0/sharer.min.js"></script><script type="text/javascript">window.config={"code":{"copyTitle":"复制到剪贴板","maxShownLines":10},"comment":{},"lightGallery":{"actualSize":false,"exThumbImage":"data-thumbnail","hideBarsDelay":2000,"selector":".lightgallery","speed":400,"thumbContHeight":80,"thumbWidth":80,"thumbnail":true},"search":{"algoliaAppID":"REQJX89W85","algoliaIndex":"index.zh-cn","algoliaSearchKey":"63fa048de9b35627f46672e95abc14df","highlightTag":"em","maxResultLength":10,"noResultsFound":"没有找到结果","snippetLength":50,"type":"algolia"}};</script><script type="text/javascript" src="/js/theme.min.js"></script></body>
</html>
